package com.jindianjichi.auth.appliccation.controller;

import com.alibaba.fastjson.JSON;
import com.google.common.base.Preconditions;
import com.jindianjichi.auth.appliccation.convert.AuthRoleDTOConvert;
import com.jindianjichi.auth.appliccation.dto.AuthRoleDTO;

import com.jindianjichi.auth.domain.BO.AuthRoleBO;
import com.jindianjichi.auth.domain.service.AuthRoleDomainService;
import com.jindianjichi.auth.entity.Result;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@Slf4j
@RequestMapping("/role")
public class AuthRoleController {
    @Autowired
    private AuthRoleDomainService authRoleDomainService;

    /**
     * 添加角色
     */
    @PostMapping("/add")
    public Result<Boolean> addRole(@RequestBody AuthRoleDTO authRoleDTO) {
        if (log.isInfoEnabled()) {
            log.info("UserController.add.dto:{}", JSON.toJSONString(authRoleDTO));
        }
        Preconditions.checkArgument(!StringUtils.isBlank(authRoleDTO.getRoleName()), "角色名称不能为空");
        Preconditions.checkNotNull(authRoleDTO.getRoleName(),"角色唯一标识不能为空");
        AuthRoleBO authRoleBO = AuthRoleDTOConvert.instance.convertToBoAuthRole(authRoleDTO);
        return Result.ok(authRoleDomainService.add(authRoleBO));

    }


    /**
     * 修改用户信息
     */
    @PostMapping("/update")
    public Result<Boolean> update(@RequestBody AuthRoleDTO authRoleDTO) {
        try {
            if (log.isInfoEnabled()) {
                log.info("UserController.update.dto:{}", JSON.toJSONString(authRoleDTO));
            }
            Preconditions.checkArgument(!StringUtils.isBlank(authRoleDTO.getRoleName()), "角色名称不能为空");
            AuthRoleBO authRoleBO = AuthRoleDTOConvert.instance.convertToBoAuthRole(authRoleDTO);
            return Result.ok(authRoleDomainService.update(authRoleBO));
        } catch (Exception e) {
            log.error("AuthRoleController.update.error:{}", e.getMessage(), e);
            return Result.fail("更新用户信息失败");
        }
    }
    /**
     * 删除用户
     * @param authRoleDTO
     * @return
     */
    @PostMapping("/delete")
    public Result<Boolean> deleteRole(@RequestBody AuthRoleDTO authRoleDTO) {
        try {
            if (log.isInfoEnabled()) {
                log.info("UserController.update.dto:{}", JSON.toJSONString(authRoleDTO));
            }
            AuthRoleBO authRoleBO = AuthRoleDTOConvert.instance.convertToBoAuthRole(authRoleDTO);
            return Result.ok(authRoleDomainService.deleteRole(authRoleBO));
        } catch (Exception e) {
            log.error("UserController.update.error:{}", e.getMessage(), e);
            return Result.fail("删除角色失败");
        }
    }
}
